<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>

          <a-col :xs="24" :sm="12">
            <a-form-item label="所在营区" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['suozyq', validatorRules.suozyq]" :trigger-change="true" dictCode="ying_qu" placeholder="请选择所在营区"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="所在楼宇" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['suozly', validatorRules.suozly]" :trigger-change="true" dictCode="hq_louy,name,id" placeholder="请选择所在楼宇"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['shebmc', validatorRules.shebmc]" placeholder="请输入设备名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="设备规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['shebggxh', validatorRules.shebggxh]" placeholder="请输入设备规格型号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="IP地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['ip', validatorRules.ip]" placeholder="请输入IP地址"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="流媒体服务器地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['rtmpdz']" placeholder="请输入流媒体服务器地址"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['beiz']" rows="4" placeholder="请输入备注"/>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="人流密度明细" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="hqStRenlmdMxTable.loading"
            :columns="hqStRenlmdMxTable.columns"
            :dataSource="hqStRenlmdMxTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
        
      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'HqStRenlmdModal',
    mixins: [JEditableTableMixin],
    components: {
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          suozyq: {
            rules: [
              { required: true, message: '请输入所在营区!'},
            ]
          },
          suozly: {
            rules: [
              { required: true, message: '请输入所在楼宇!'},
            ]
          },
          shebmc: {
            rules: [
              { required: true, message: '请输入设备名称!'},
            ]
          },
          shebggxh: {
            rules: [
              { required: true, message: '请输入设备规格型号!'},
            ]
          },
          ip: {
            rules: [
              { required: true, message: '请输入IP地址!'},
            ]
          },
        },
        refKeys: ['hqStRenlmdMx', ],
        tableKeys:['hqStRenlmdMx', ],
        activeKey: 'hqStRenlmdMx',
        // 人流密度明细
        hqStRenlmdMxTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: 'ip地址',
              key: 'ip',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '进入人数',
              key: 'jinrrs',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '离开人数',
              key: 'likrs',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '上报时间',
              key: 'shangbsj',
              type: FormTypes.datetime,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        url: {
          add: "/renlmd/hqStRenlmd/add",
          edit: "/renlmd/hqStRenlmd/edit",
          hqStRenlmdMx: {
            list: '/renlmd/hqStRenlmd/queryHqStRenlmdMxByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'suozyq','suozly','shebmc','shebggxh','ip','rtmpdz','beiz')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.hqStRenlmdMx.list, params, this.hqStRenlmdMxTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          hqStRenlmdMxList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'suozyq','suozly','shebmc','shebggxh','ip','rtmpdz','beiz'))
     },

    }
  }
</script>

<style scoped>
</style>